package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.ActivityDto;
import com.qk.entity.Activity;
import com.qk.mapper.ActivityMapper;
import com.qk.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityMapper ActivityMapper;

    @Override
    public PageResult<Activity> list(ActivityDto activityDto) {
        // 1.设置分页参数
        PageHelper.startPage(activityDto.getPage(), activityDto.getPageSize());

        // 2.调用mapper接口方法,执行查询
        List<Activity> activityList = ActivityMapper.selectList(activityDto);

        // 3.解析并封装分页结果
        Page<Activity> page = (Page<Activity>) activityList;
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    @Override
    public void add(Activity activity) {
        activity.setCreateTime(LocalDateTime.now());
        activity.setUpdateTime(LocalDateTime.now());
        ActivityMapper.insert(activity);
    }

    @Override
    public Activity getById(Integer id) {
        return ActivityMapper.selectById(id);
    }

    @Override
    public void update(Activity activity) {
        ActivityMapper.update(activity);
    }

    @Override
    public void deleteById(Integer id) {
        ActivityMapper.deleteById(id);
    }

    @Override
    public List<Activity> getBySubject(Integer type) {
        return ActivityMapper.selectByType(type);
    }
}
